﻿using System.Collections.Generic;
using UnityEngine;
using TinyWnd;

public class LevelWnd : Wnd
{
    [SerializeField]
    GameObject prefabLevel;

    Transform tranLevels;
    TweenAlpha twAlpha;

    protected override void OnAwake()
    {
        _wndId = WndId.level;
        _unifiedClose = true;
        _colliderMode = WndColliderMode.transparent;

        var tran = transform;
        tranLevels = Utility.FindChild(tran, "Bg/Levels");
        twAlpha = Utility.FindChild<TweenAlpha>(tran, "Bg");

        SetReturnCallback(CbReturn);
    }

    protected override void OnShow(WndContentData content)
    {
        gameObject.SetActive(true);
        twAlpha.onFinished.Clear();
        twAlpha.PlayForward();

        FillLevel();
    }

    protected override void OnHide()
    {
        twAlpha.SetOnFinished(CbFinishClose);
        twAlpha.PlayReverse();
    }

    void CbFinishClose()
    {
        gameObject.SetActive(false);
    }

    void FillLevel()
    {
        int i = 1;
        foreach (Transform child in tranLevels)
        {
            if (child.childCount > 0)
                continue;
            GameObject item = NGUITools.AddChild(child.gameObject, prefabLevel);
            WLevelTemp script = item.GetComponent<WLevelTemp>();
            script.InitLevel(i++);
        }
    }

    bool CbReturn()
    {
        var content = new MsgBoxContent();
        content.boxType = MsgBoxType.okCancel;
        content.text = "Close level window?";
        content.cbOk = delegate
        {
            Close();
        };
        WndMgr.inst.ShowWindow(WndId.msgBox, content);
        return false;
    }
}
